package com.javasm.commons;


import com.alibaba.fastjson.JSONObject;
import com.javasm.entity.PermissionAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private PermissionAuth permissionAuth;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!permissionAuth.check(request.getRequestURI())){
            response.setContentType("application/json;charset=utf-8");
            Map<String,Object> map = new HashMap<>();
            map.put("code",403);
            map.put("success",false);
            map.put("msg","没有权限");
            PrintWriter writer = response.getWriter();
            writer.write(JSONObject.toJSONString(map));
            writer.flush();
            writer.close();
        }
        return true;
    }
}
